import { PolygonUtil } from "../Common/Util/PolygonUtil";
import TextureSliceRender from "./TextureSliceRender";

const { ccclass, menu, property } = cc._decorator;

@ccclass
@menu('TextureSlice/TextureSlice')
export default class TextureSlice extends cc.Component {

    /** 纹理节点 */
    @property({ type: cc.Node, tooltip: '纹理节点' })
    textureNode: cc.Node = null;

    /** 起始触摸点 */
    startPoint: cc.Vec2 = null;

    onLoad() {
        this.addTouchEmit();
    }

    start() { }

    onTouchStart(event: cc.Event.EventTouch) {
        this.startPoint = event.getLocation();
        // console.log(this.startPoint);
        // this.startPoint = new cc.Vec2(this.node.width / 2 - 500, this.node.height / 2);
    }

    onTouchMove(event: cc.Event.EventTouch) { }

    onTouchEnd(event: cc.Event.EventTouch) {
        let endPoint = event.getLocation();
        // endPoint = new cc.Vec2(this.node.width / 2 + 500, this.node.height / 2);

        this.dividePolygon(this.startPoint, endPoint);
    }

    /**
     * 根据起点与终点的线段, 分割四边形
     * @param p0 起点
     * @param p1 终点
     * @param isWorld 触摸点是否是世界坐标
     */
    dividePolygon(p0: cc.Vec2, p1: cc.Vec2, isWorld = true) {
        let textureRender = this.textureNode.getComponent(TextureSliceRender);

        // console.log('texture', textureRender.texture);
        let pa = p0.clone();
        let pb = p1.clone();
        if (isWorld) {
            pa = this.textureNode.convertToNodeSpaceAR(p0);
            pb = this.textureNode.convertToNodeSpaceAR(p1);
        }
        // for (let i = 0; i < textureRender.polygon.length; i++) {
        //     console.log('原 polygon', textureRender.polygon[i].x, textureRender.polygon[i].y);
        // }
        let polygons = PolygonUtil.lineCutPolygon(pa, pb, textureRender.polygon);
        if (polygons.length <= 0) return;
        this.splitTexture(textureRender, polygons);
    }

    splitTexture(texture: TextureSliceRender, polygons: cc.Vec2[][]) {
        texture.polygon = polygons[0];
        
        // for (let i = 0; i < polygons.length; i++) {
        //     for (let k = 0; k < polygons[i].length; k++) {
        //         console.log('后 polygon', polygons[i][k].x, polygons[i][k].y);
        //     }
        // }
        
        for (let i = 1; i < polygons.length; i++) {
            let node = new cc.Node();
            let t = node.addComponent(TextureSliceRender);
            node.parent = this.node;
            node.x = texture.node.position.x;
            node.y = texture.node.position.y - 30;
            t.texture = this.textureNode.getComponent(TextureSliceRender).texture;
            // console.log('后 polygon', polygons[i]);
            t.polygon = polygons[i];
        }
    }

    addTouchEmit() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }
}
